<template>
  <div>
    <el-dialog
      width="1024px"
      v-bind="$attrs"
      v-on="$listeners"
      :close-on-click-modal="false"
      :show-close="true"
      @open="onOpen"
      @close="onClose"
    >
      <div slot="title">
        {{ formData.id != null ? '修改客户' : '新增客户' }}
      </div>
      <div class="sketch_content">
        <el-form label-position="top" ref="elForm" :model="formData" :rules="rules" label-width="80px">
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="客户名称" prop="customerName">
                <el-input v-model="formData.customerName" style="width: 100%"/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="客户来源" prop="sourceId">
                <el-select v-model="formData.sourceId" placeholder="请选择" style="width: 100%">
                  <el-option
                    v-for="dict in dict.type.crm_source_name"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="手机" prop="mobile">
                <el-input v-model="formData.mobile" prefix-icon="el-icon-mobile-phone" style="width: 100%"/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="电话" prop="telephone">
                <el-input v-model="formData.telephone" prefix-icon="el-icon-phone-outline" style="width: 100%"/>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="网址" prop="website">
                <el-input v-model="formData.website" prefix-icon="el-icon-message" style="width: 100%"/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="邮箱" prop="email">
                <el-input v-model="formData.email" prefix-icon="el-icon-discover" style="width: 100%"/>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="客户行业" prop="industryId">
                <el-select v-model="formData.industryId" placeholder="请选择" style="width: 100%">
                  <el-option
                    v-for="dict in dict.type.crm_customer_industry"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="客户级别" prop="levelId">
                <el-select v-model="formData.levelId" placeholder="请选择" style="width: 100%">
                  <el-option
                    v-for="dict in dict.type.crm_customer_level"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="下次联系时间" prop="nextTime">
                <el-date-picker value-format="yyyy-MM-dd hh:mm:ss" v-model="formData.nextTime" type="datetime"
                                placeholder="请选择" style="width: 100%"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="定位">
                <amap :default-value.sync="formData.location" @getPosition="getPosition"></amap>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="详细位置">
                <el-input
                  type="textarea"
                  :autosize="{ minRows: 2, maxRows: 5}"
                  placeholder="定位详细位置"
                  v-model="formData.detailAddress"
                >
                </el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="24">
              <el-form-item label="备注" prop="remark">
                <el-input
                  type="textarea"
                  :autosize="{ minRows: 3, maxRows: 5}"
                  placeholder="请输入内容"
                  v-model="formData.remark"
                >
                </el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" type="primary" @click="submitForm" :loading="submitLoading">保 存</el-button>
        <el-button size="small" @click="onClose">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  selectByPrimaryKey,
  saveHasKey,
  updateByPrimaryKeySelective
} from '@/api/crm/Customer'

import Amap from '@/components/AMap/Index'

export default {
  inheritAttrs: false,
  name: 'CustomerCreate',
  dicts: ['crm_source_name', 'crm_customer_industry', 'crm_customer_level'],
  components: { Amap },
  props: {
    pk: {
      type: Number,
      default: null
    }
  },
  data() {
    return {
      title: '添加客户', // 对话框标题
      dialogVisible: false, // 对话框懒加载
      submitLoading: false, // 提交按钮加载动画
      formData: {
        id: null, // 主键id
        customerName: '水果妹便利店', // 客户名称
        sourceName: '', // 客户来源
        sourceId: '', // 客户来源id
        mobile: '13244447789', // 手机
        telephone: '123654987', // 电话
        website: 'www.shuiguomei.com', // 网址
        email: 'shuiguomei@123.com', // 邮箱
        industryName: '', // 客户行业
        industryId: '', // 客户行业id
        levelName: '', // 客户级别
        levelId: '', // 客户级别id
        nextTime: null, // 下次联系时间
        remark: '只要水果和饮品', // 备注
        location: '', // 地区定位
        detailAddress: '', // 详细地址
        address: '', // 省市区
        lng: null, // 经度
        lat: null // 纬度
      },
      // 规则
      rules: {
        customerName: [{ required: true, message: '客户名称不能为空', trigger: 'blur' }]
      }
    }
  },
  // 在实例创建完成后被立即调用
  created() {
  },
  // 实例被挂在后调用
  mounted() {

  },
  // 方法
  methods: {
    // 对话框关闭回调
    onClose() {
      this.submitLoading = false
      this.$refs['elForm'].resetFields()
      this.$emit('update:visible', false)
    },
    // 提交
    submitForm() {
      this.submitLoading = true
      this.$refs['elForm'].validate(valid => {
        if (valid) {
          if (this.formData.id != undefined) {
            // 修改
            updateByPrimaryKeySelective(this.formData).then(res => {
              this.$modal.msgSuccess('修改成功')
              this.$emit('ok')
              this.onClose()
              this.submitLoading = false
            })
          } else {
            // 新增
            saveHasKey(this.formData).then(res => {
              this.$modal.msgSuccess('新增成功')
              this.$emit('ok')
              this.onClose()
              this.submitLoading = false
            })
          }
        }
      })
    },
    // 打开对话框
    onOpen() {
      if (this.pk != null) {
        selectByPrimaryKey(this.pk).then(res => {
          this.formData = res.data
        })
      }
    },
    // 获取地址信息
    getPosition(item) {
      this.formData.lng = item.location.split(',')[0]
      this.formData.lat = item.location.split(',')[1]
      this.formData.detailAddress = item.provinceName + item.cityName + item.districtName + item.name + item.value
      this.formData.location = item.value
    }
  }
}
</script>

<style scoped>

</style>
